var express = require('express');
var router = express.Router();

let mysql = require("mysql");
const db = require('./mysql/mysql');

var connection = mysql.createConnection({
    // host: '192.168.1.194',
    // port: 3306, // MySQL 默认端口为 3306
    // user: 'qwn',  //用户名
    // password: 'qwn123', //用户密码
    // database: 'orderdata',  //数据库
        host     : 'localhost',
      port: 3306, // MySQL 默认端口为 3306
      user     : 'root',  //用户名
      password : 'woaini.1.2', //用户密码
      database : 'orderdata',  //数据库
    useConnectionPooling: true
})

// connection.connect();

// 学员数据请求
router.get("/userStudent", (req, res, next) => {
    connection.query("SELECT * from user", (error, results, fields) => {
        if (error) throw error;
        let student = results.filter((item) => {
            return item.type === "0";
        });

        console.log(req.query);
        res.send(student)
    })
})
// 删除学员数据
router.post("/deleteStudent", (req, res, next) => {
    connection.query(`DELETE FROM user WHERE id = ${req.body.id}`, (error, results, fields) => {
        if (error) throw error;
        res.send("删除成功")
    })
})
// 添加讲师数据
router.post("/addlecturer", (req, res, next) => {
    connection.query(`insert into user set ?`, req.body.obj, (error, results, fields) => {
        if (error) throw error;
        res.send("添加")
    })
})
// 添加讲师数据
router.post('/addlecture',(req,res,next) => {
    // console.log(req.body);
    console.log(`insert into reactteacherdata set ?`,req.body);
    connection.query(`insert into reactteacherdata set ?`,req.body,(err,rows,fields)=>{
        if(err) throw err;
        res.send('添加成功')
    })
})
// 删除讲师数据
router.post("/deletelecturer", (req, res, next) => {
    connection.query(`DELETE FROM reactteacherdata WHERE id = ${req.body.id}`, (error, results, fields) => {
        if (error) throw error;
        res.send("删除成功")
    })
})

// 用户禁用
router.post("/userDisabled", (req, res, next) => {
    let { id, userDisable } = req.body
    connection.query(`update user set userDisable=${userDisable ? false : true} where id="${id}"
    `, (error, results, fields) => {
        if (error) throw error;
        res.send("更改成功")
    })
})

// 全选
router.post("/checkAll", (req, res, next) => {
    let { boo, type } = req.body
    console.log(boo, type);
    console.log(type);
    connection.query(`update user set isChecked=${boo} where type=${type}
    `, (error, results, fields) => {
        if (error) throw error;
        res.send("更改成功")
    })
})
// 学员编辑
router.post("/upDatestudent", (req, res, next) => {
    let { id, name, phone } = req.body
    console.log(id, name, phone);
    console.log(req.body);
    connection.query(`update user set user_name="${name}",phone="${phone}" WHERE id = ${id}`, (error, results, fields) => {
        if (error) throw error;
        res.send("更改")
    })
})

// 讲师编辑
router.post("/upDatelecturer", (req, res, next) => {
    let { id, teacher, trait } = req.body
    console.log(req.body);
    connection.query(`update reactteacherdata set teacher="${teacher}",trait="${trait}" WHERE id = ${id}`, (error, results, fields) => {
        if (error) throw error;
        res.send("更改")
    })
})


module.exports = router;